#include "serverapplication.h"
#include <QMessageBox>

ServerApplication::ServerApplication ( int & argc, char ** argv )
    : QApplication(argc, argv)
{
    connect (this, SIGNAL(serverCreateFailed(QString)), this, SLOT(quit()));
    dbInterface = new DatabaseInterface(this);
    server = new NetworkServer(this);

    if(!server->listen()) {
        emit serverCreateFailed(server->errorString());
        return;
    }

    connect (server, SIGNAL(listeningAt(QHostAddress*,quint16)), this, SLOT(showIPPort(QHostAddress*, quint16)));
    server->start();
}

ServerApplication::~ServerApplication()
{
    delete dbInterface;
    delete server;
}

void ServerApplication::showIPPort(QHostAddress *address, quint16 port)
{
    QString message = "Server is available at IP: ";
    message.append(address->toString()).append(" Port: ").append(QString::number(port));
    QMessageBox::information(0, tr("Server Listening"), message);
}
